package root.LocalityBasedLeastConnectionswithReplicationScheduling;

import java.util.Arrays;
import java.util.List;

// Main 类，测试 LBC-RS 算法
public class LBCRSExample {
    public static void main(String[] args) {
        // 创建服务器
        Server server1 = new Server("Server1", "US-East");
        Server server2 = new Server("Server2", "US-West");
        Server server3 = new Server("Server3", "US-East");
        Server server4 = new Server("Server4", "EU-Central");

        // 将服务器加入调度器
        List<Server> servers = Arrays.asList(server1, server2, server3, server4);
        Scheduler scheduler = new Scheduler(servers);

        // 创建请求
        Request request1 = new Request("US-East");
        Request request2 = new Request("EU-Central");

        // 调度请求
        List<Server> scheduledServers1 = scheduler.scheduleRequest(request1);
        System.out.println("Request 1 scheduled to: " + scheduledServers1.get(0).getName() + " and its replica: " + scheduledServers1.get(1).getName());

        List<Server> scheduledServers2 = scheduler.scheduleRequest(request2);
        System.out.println("Request 2 scheduled to: " + scheduledServers2.get(0).getName() + " and its replica: " + scheduledServers2.get(1).getName());

        // 完成请求，释放连接
        scheduler.completeRequest(scheduledServers1.get(0));
        scheduler.completeRequest(scheduledServers1.get(1));
        scheduler.completeRequest(scheduledServers2.get(0));
        scheduler.completeRequest(scheduledServers2.get(1));
    }
}
